package com.carrental.api.ums.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrental.api.common.query.CommonQueryParam;
import com.carrental.api.common.utils.CommonQueryWrapper;
import com.carrental.api.ums.domain.UmsFeedback;
import com.carrental.api.ums.domain.UmsFeedback;
import com.carrental.api.ums.domain.bo.UmsFeedbackBo;
import com.carrental.api.ums.domain.bo.UmsFeedbackBo;
import com.carrental.api.ums.domain.vo.UmsFeedbackVo;
import com.carrental.api.ums.domain.vo.UmsFeedbackVo;
import com.carrental.api.ums.mapper.UmsFeedbackMapper;
import com.carrental.api.ums.service.IUmsFeedbackService;
import com.carrental.common.exception.OperationException;
import com.carrental.common.utils.StpUmsUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 反馈反馈信息Service业务层处理
 *
 * @author liangyi
 * @date 2022-03-27
 */
@Service
public class UmsFeedbackServiceImpl extends ServiceImpl<UmsFeedbackMapper,UmsFeedback> implements IUmsFeedbackService {


    /**
     * 增加反馈
     *
     * @param feedbackBo bo
     * @return is
     */
    @Override
    public Boolean create(UmsFeedbackBo feedbackBo) {
        UmsFeedback feedback = new UmsFeedback();
        BeanUtils.copyProperties(feedbackBo, feedback);
        if (!this.save(feedback)) {
            throw new OperationException("新增反馈失败！");
        }
        return true;
    }

    /**
     * 删除反馈
     *
     * @param id id
     * @return is
     */
    @Override
    public Boolean delete(Long id) {
        UmsFeedback feedback = new UmsFeedback();
        feedback.setIsDeleted(true);
        if (!this.updateById(feedback)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 增加反馈
     *
     * @param feedbackBo bo
     * @return is
     */
    @Override
    public Boolean update(Long id, UmsFeedbackBo feedbackBo) {
        UmsFeedback feedback = new UmsFeedback();
        BeanUtils.copyProperties(feedbackBo, feedback, "feedback_id");
        if (!this.updateById(feedback)) {
            throw new OperationException("删除失败！");
        }
        return true;
    }

    /**
     * 增加反馈
     *
     * @param id bo
     * @return is
     */
    @Override
    public UmsFeedbackVo detail(Long id) {
        return this.baseMapper.detail(
            new QueryWrapper<UmsFeedback>()
                .lambda()
                .eq(UmsFeedback::getFeedbackId, id)
        );
    }

    /**
     * 反馈分页
     *
     * @param commonQueryParam 查询参数
     * @return is
     */
    @Override
    public IPage<UmsFeedbackVo> feedbackPage(CommonQueryParam commonQueryParam) {
        return this.baseMapper.pageList(
            new Page<>(commonQueryParam.getCurrent(), commonQueryParam.getSize()),
            new CommonQueryWrapper<UmsFeedback>(commonQueryParam)
                .lambda()
                .eq(UmsFeedback::getIsDeleted, false)
                .orderByDesc(UmsFeedback::getCreateTime)
        );
    }

    /**
     * 反馈列表
     *
     * @param commonQueryParam 查询参数
     * @return list
     */
    @Override
    public List<UmsFeedbackVo> feedbackList(CommonQueryParam commonQueryParam) {
        return this.baseMapper.pageList(
            new CommonQueryWrapper<UmsFeedback>(commonQueryParam)
                .lambda()
                .eq(UmsFeedback::getIsDeleted, false)
                .orderByDesc(UmsFeedback::getCreateTime)
        );
    }
}
